package com.github.bluzwang.fluxsupport;


import android.util.Log;
import de.greenrobot.event.EventBus;

import java.util.HashMap;
import java.util.Map;

//import javax.inject.Inject;

/**
 * Created by lgvalle on 19/07/15.
 */
public class Dispatcher {
    private final EventBus bus;

    //@Inject
    public Dispatcher(EventBus bus) {
        this.bus = bus;
    }

    public void register(final Object cls) {
        bus.register(cls);
    }

    public void unregister(final Object obj) {
        bus.unregister(obj);
    }

    public ActionBuilder dispatchRequestToStoreWithType(String type) {
        return new ActionBuilder().with(type);
    }

    public class ActionBuilder {
        private String type;
        private Map<String, Object> data;
        //private Map<String, Class> dataClass;

        ActionBuilder with(String type) {
            if (type == null) {
                throw new IllegalArgumentException("Type may not be null.");
            }
            this.type = type;
            this.data = new HashMap<>();
            //this.dataClass = new Hashtable<>();
            return this;
        }

        public ActionBuilder and(String key, Object value) {
            if (key == null) {
                throw new IllegalArgumentException("Key may not be null.");
            }

            if (value == null) {
                throw new IllegalArgumentException("Value may not be null.");
            }
            Log.d("bruce", "value: " + value + " class" + value.getClass().getSimpleName());
            data.put(key, value);
            //dataClass.put(key, value.getClass());
            return this;
        }

        public void post() {
            if (type == null || type.isEmpty()) {
                throw new IllegalArgumentException("At least one key is required.");
            }
            postToBus(new Action(type, data/*, dataClass*/));
        }
    }


    public ResponseBuilder postResponseToUIWithType(String type) {
        return new ResponseBuilder().with(type);
    }

    public class ResponseBuilder {
        private String type;
        private Map<String, Object> data;

        ResponseBuilder with(String type) {
            if (type == null) {
                throw new IllegalArgumentException("Type may not be null.");
            }
            this.type = type;
            this.data = new HashMap<>();
            return this;
        }

        public ResponseBuilder and(String key, Object value) {
            if (key == null) {
                throw new IllegalArgumentException("Key may not be null.");
            }
            if (value == null) {
                throw new IllegalArgumentException("Value may not be null.");
            }
            Log.d("bruce", "value: " + value + " class" + value.getClass().getSimpleName());
            data.put(key, value);
            //dataClass.put(key, value.getClass());
            return this;
        }

        public void post() {
            if (type == null || type.isEmpty()) {
                throw new IllegalArgumentException("At least one key is required.");
            }
            postToBus(new Response(type, data/*, dataClass*/));
        }
    }

    private boolean isEmpty(String type) {
        return type == null || type.isEmpty();
    }

    private void postToBus(final Object event) {
        bus.post(event);
    }
}
